﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using webnew.Common;
using webnew.BLL;
namespace webnew.Systems
{
    public partial class S_deptEdit : WebPageBase.EditBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //设置列表页面名，即 返回的页面名

            _ListSrc = "S_deptList.aspx";//?settype=" + Request.QueryString["settype"].ToString() +"
            //UcCompany3.SelectedIndexChanged += UcCompany3_SelectedIndexChange;
            //UcCompany3.AutoPostBack = true;
            #region 隐藏母板页面按钮
            this.Master.FindControl("btnSaveBack").Visible = true;
            this.Master.FindControl("btnSaveStay").Visible = true;
            this.Master.FindControl("btnBack").Visible = true;

            #endregion

            if (!IsPostBack)
            {
                //设置页面标题
                SetTitle(true, "后台管理 >> 部门管理");
                webnew.Common.CommonHelper.DataDdlBind(ddlCompLevel, new BLL.Sys_SystemType().GetconList("系统单位级别"), "TypeName", "TypeName");
                ddlCompLevel_SelectedIndexChanged(null, null);
                webnew.Common.CommonHelper.DataDdlBind(ddlDeptLevel, new BLL.Sys_SystemType().GetconList("系统部门级别"), "TypeName", "TypeName");
   
            }
        }


        /// <summary>
        /// 单位级别选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlCompLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
              Common.CommonHelper.DataDdlBind(ddlCompany, new BLL.Sys_Company().GetLevelCompany(ddlCompLevel.SelectedValue), "compname", "id");
              ddlCompany.Items.Insert(0, new ListItem("--请选择--", Guid.Empty.ToString()));    
        }

        /// <summary>
        /// 部门级别选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlDeptLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlDeptLevel.SelectedValue != "一级部门")
            {

                Common.CommonHelper.DataDdlBind(ddlParentID, new BLL.Sys_Dept().GetDeptByCompLev(ddlCompany.SelectedValue, "一级部门"), "deptname", "id");
                pnlParentId.Visible = true;

            }
            else
            {
                pnlParentId.Visible = false;
            }
        }

 
        /// <summary>
        /// 重写基类，显示数据方法
        /// </summary>
        protected override void ShowData()
        {          
            Model.Sys_DeptInfo model = new BLL.Sys_Dept().GetList(_RecordId);
            txtDeptSort.Text = model.sort.ToString();                 
            txtDeptName.Text = model.deptname;
            ddlCompany.SelectedValue = model.Compid;
            ddlDeptLevel.SelectedValue = model.DeptLevel ;
            ddlParentID.SelectedValue = model.ParentID;  //父ID
            ViewState["currentDeptName"] = model.deptname; //保存当前部门名,用于验证判断
            txtRemark.Text = model.remark;
            
        }

        /// <summary>
        /// 重写基类，传入要验证的数据
        /// </summary>
        protected override List<PageValid> _SetValidData()
        {
            List<PageValid> lst = new List<PageValid>();
            lst.Add(new PageValid("序号", this.txtDeptSort.Text.Replace(" ", ""), PageValid.validType.CommonValid, false, 5));
            lst.Add(new PageValid("序号", txtDeptSort.Text.Replace(" ", ""), PageValid.validType.IntValid, "只能为正整数"));
            lst.Add(new PageValid("序号", "0", txtDeptSort.Text.Replace(" ", ""), PageValid.validType.NoSameValid, "不能为0数字"));
            lst.Add(new PageValid("部门名称", this.txtDeptName.Text, PageValid.validType.CommonValid, false, 25));
            lst.Add(new PageValid("备注", this.txtRemark.Text, PageValid.validType.CommonValid, true, 500));
            lst.Add(new PageValid("请选择", Guid.Empty.ToString(), ddlCompany.SelectedValue, PageValid.validType.NoSameValid, "所属单位"));
            ////判断是否存在相同的单位名
            if (_RecordId != null) //修改
            {
                //判断用户在修改状态下是否修改过单位名称,没修改过 则跳过验证
                if (ViewState["currentDeptName"].ToString() != txtDeptName.Text)
                    lst.Add(new PageValid("部门名称", "1", new BLL.Sys_Dept().ExistDeptName(txtDeptName.Text, ddlDeptLevel.SelectedValue,ddlCompany.SelectedValue), PageValid.validType.NoSameValid, "已存在,请重新输入"));
            }
            else //添加
                lst.Add(new PageValid("部门名称", "1", new BLL.Sys_Dept().ExistDeptName(txtDeptName.Text, ddlDeptLevel.SelectedValue, ddlCompany.SelectedValue), PageValid.validType.NoSameValid, "已存在,请重新输入"));

            return lst;
        }


        /// <summary>
        /// 重写基类保存数据方法
        /// </summary>
        protected override int SaveData()
        {
            Model.Sys_DeptInfo model = new Model.Sys_DeptInfo();
            model.Id = _RecordId;
            model.Compid = ddlCompany.SelectedValue;
            model.DeptLevel = ddlDeptLevel.SelectedValue;
            model.ParentID = ddlParentID.SelectedValue ==""? Guid.Empty.ToString() : ddlParentID.SelectedValue;
            model.deptname = txtDeptName.Text;
           
            model.sort = MyConvert.toInt32(txtDeptSort.Text);
            model.remark = txtRemark.Text;
            model.isdel = 0;

            //----------系统日志model

            Model.Sys_LogInfo modelLog = new webnew.Model.Sys_LogInfo();
            modelLog.UserID = UserInfor.Id;                  //用户ID
            modelLog.IP = new BLL.Sys_Log().GetUserIP(System.Web.HttpContext.Current.Request);  //IP
            modelLog.Module = new BLL.Sys_Navigation().GetModuleName(this.GetType().BaseType.Name);   //模块名
            modelLog.CreateDate = DateTime.Now;           //创建时间

            string error = "";                   //获取错误信息

            if (_RecordId !=null)  //修改
            {
                try
                {
                    return new BLL.Sys_Dept().Update(model);
                }
                catch (Exception e)
                {
                    error = e.ToString();
                }
                finally
                {
                    //添加一条系统日志
                    new BLL.Sys_Log().AddSysLog(error, "修改", modelLog, "系统部门", "部门，部门名称为" + txtDeptName.Text);
                }
            }

            else                //添加
            {
                //if (txtDeptSort.Text.Trim().Equals("")) model.sort = 1; //如果序号为空，则序号默认为1              
                try
                {
                    return new BLL.Sys_Dept().Add(model);
                }
                catch (Exception e)
                {
                    error = e.ToString();
                }
                finally
                {
                    //添加一条系统日志
                    new BLL.Sys_Log().AddSysLog(error, "添加", modelLog, "系统部门", "部门，部门名称为" + txtDeptName.Text);
                }
            }

                //弹出文本框成功或者失败
                if (error == "") { return 1; }       //成功
                else { return 0; }                  //失败
            
        }

       
    }
}
